//
//  ViewController.swift
//  AnimationTest
//
//  Created by jansh on 16/4/15.
//  Copyright © 2016年 jansh. All rights reserved.
//

import UIKit



class ViewController: UIViewController {
    
    var orangeView:UIView!
    var blackView:UIView!
    
    var label:UILabel!
    
    let RotateAnimationKey = "rotateAnimation"
    var rotateAnimation: CAAnimation!
    
    let scaleAnimationDuration:NSTimeInterval = 1.0
    let rotateAnimationDuration:NSTimeInterval = 3.0
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        
//        orangeView = UIView.init(frame: CGRectMake(40, 100, 100, 100))
//        orangeView.backgroundColor = UIColor.orangeColor()
//        view.addSubview(orangeView)
//        
//        blackView = UIView.init(frame: CGRectMake(150, 100, 100, 100))
//        blackView.backgroundColor = UIColor.blackColor()
//        view.addSubview(blackView)
//        
////        let scaleGroupAnimationDuration = rotateAnimationDuration / 2.0 + scaleAnimationDuration
////        let rotateGroupAnimationDuration = rotateAnimationDuration + scaleAnimationDuration * 2.0
//        
//        self.orangeViewAnimation()
//        self.blackViewAnimation()

//        label = UILabel.init(frame: CGRectMake(10, 10, 300, 30))
//        label.center = CGPointMake(UIScreen.mainScreen().bounds.size.width/2, UIScreen.mainScreen().bounds.size.height/2)
//        label.text = "Rotation Animation Test";
//        label.tag = 10010
//        view.addSubview(label)
//        
//        let button:UIButton = UIButton(type: .Custom)
//        button.frame = CGRectMake(10, UIScreen.mainScreen().bounds.height-40, 300, 30)
//        button.backgroundColor = UIColor.redColor()
//        // Selector 如果不把方法名以 string 的形式传进去的话,会出现直接进行调用的情况
//        button.addTarget(self, action: Selector("rotateAnimationWithLabel"), forControlEvents: .TouchUpInside)
//        view.addSubview(button)
//        setUpNotification()
// MARK: 压缩和解压缩
        
    }
    
    func rotateAnimationWithLabel() {
        let basicAnimation = CABasicAnimation(keyPath: "transform.rotation")
        basicAnimation.fromValue = -M_PI*2
        basicAnimation.duration = 1.0
        
        let groupAnimation = CAAnimationGroup()
        groupAnimation.beginTime = CACurrentMediaTime()
        groupAnimation.duration = 1.0
        groupAnimation.repeatCount = Float.infinity
        groupAnimation.animations = [basicAnimation]
        label.layer.addAnimation(groupAnimation, forKey: RotateAnimationKey)
//        startAnimation()
    }
    
    private func setUpNotification() {
        NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("applicationWillEnterForegroundNotification"), name: UIApplicationWillEnterForegroundNotification, object: nil)
        NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("applicationDidEnterBackgroundNotification"), name: UIApplicationDidEnterBackgroundNotification, object: nil)
    }
    
    func applicationWillEnterForegroundNotification() {
        if let rotateAnimation = rotateAnimation {
            label.layer.addAnimation(rotateAnimation, forKey: RotateAnimationKey)
        }
        rotateAnimation = nil
    }
    
    func applicationDidEnterBackgroundNotification() {
        rotateAnimation = label.layer.animationForKey(RotateAnimationKey)
    }
    
    // 执行旋转的橘色视图
    private func orangeViewAnimation() {
//        let rotateAnimation = CABasicAnimation(keyPath: "transform.rotation")
//        rotateAnimation.fromValue = -M_PI * 2
//        rotateAnimation.duration = rotateAnimationDuration
//        
//        let groupAnimation = CAAnimationGroup()
//        groupAnimation.beginTime = CACurrentMediaTime() + scaleAnimationDuration
//        groupAnimation.duration = rotateAnimationDuration + scaleAnimationDuration * 2
//        groupAnimation.repeatCount = Float.infinity
//        groupAnimation.animations = [rotateAnimation]
//        orangeView.layer.addAnimation(groupAnimation, forKey: "rotateAnimation")
        startAnimation()
    }
    
    // 执行缩放的黑色视图
    private func blackViewAnimation() {
        let scaleAnimation = CABasicAnimation(keyPath: "transform.scale")
        scaleAnimation.fromValue = 1
        scaleAnimation.toValue = 0.5
        // 因为在放大以后，要停留在放大的状态，等待橘色视图旋转，所以使用 fillMode 和 removedOnCompletion 两个属性来将动画停留在最终的状态
        scaleAnimation.fillMode = kCAFillModeForwards
        scaleAnimation.removedOnCompletion = false
        scaleAnimation.duration = scaleAnimationDuration
        
        let groupAnimation = CAAnimationGroup()
        groupAnimation.duration = scaleAnimationDuration + rotateAnimationDuration / 2.0
        groupAnimation.repeatCount = Float.infinity
        groupAnimation.autoreverses = true
        groupAnimation.animations = [scaleAnimation]
        blackView.layer.addAnimation(groupAnimation, forKey: "scaleAnimation")
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}

extension ViewController {
    private func pauseLayer() {
        let pausedTime = label.layer.convertTime(CACurrentMediaTime(), fromLayer: nil)
        label.layer.speed = 0.0
        label.layer.timeOffset = pausedTime
    }
    
    private func resumeLayer() {
        let pausedTime = label.layer.timeOffset
        label.layer.speed = 1.0;
        label.layer.timeOffset = 0.0;
        label.layer.beginTime = 0.0;
        let timeSincePause = label.layer.convertTime(CACurrentMediaTime(), fromLayer: nil) - pausedTime
        label.layer.beginTime = timeSincePause;
    }
}

extension ViewController {
    private func startAnimation() {
        let rotateAnimation = CABasicAnimation(keyPath: "transform.rotation")
        rotateAnimation.toValue = 2.0 * M_PI
        rotateAnimation.repeatCount = Float.infinity
        rotateAnimation.duration = 2
        label.layer.addAnimation(rotateAnimation, forKey: RotateAnimationKey)
    }
}
